﻿using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ader.TemplateEngine;
using MonitoringServer.MonitoringServiceReference;
using MonitoringService;

namespace MonitoringServer
{
  public static class Helper
  {
    /// <summary>
    /// Current user name
    /// </summary>
    public static string UserName
    {
      get
      {
        return (string) HttpContext.Current.Session[Constants.CurrentUser];
      }
    }

    /// <summary>
    /// Curremt user password (hashed)
    /// </summary>
    public static string Password
    {
      get
      {
        return (string)HttpContext.Current.Session[Constants.CurrentPassword];
      }
    }

    public static string CreateInfoHtml(Marker marker, Entity rootEntity, Entity[] subEntities)
    {
      string sourcePath = HttpContext.Current.Server.MapPath("App_Data//MarkerInfo.html");
      TemplateManager mngr = TemplateManager.FromFile(sourcePath);
      mngr.SetValue("marker", marker);
      mngr.SetValue("entities", subEntities);
      string source = File.ReadAllText(sourcePath);
      string markerInfo = GetMarkerInfo(marker);
      source = source.Replace("<MarkerInfo/>", markerInfo);
      return source;
    }

    private static string GetMarkerInfo(Marker marker)
    {
      return "Name; Coordinates; Speed; Last update";
    }

    public static SqlConnection Connection
    {
      get
      {
        SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlServer"].ConnectionString);
        cnn.Open();
        return cnn;
      }
    }

  }
}
